package com.test.mchelepesa.dialog_rice.rate;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.ServiceFeedbackActivity_rice;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseApplication_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.util_rice.AppReviewUtil;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.bean_rice.rate_rice.ActiveWindowConfig_rice;

public class StarActiveDialog_rice extends Dialog {


    private final AppCompatActivity activity;
    private final String url = "https://play.google.com/store/apps/details?id=" + BaseApplication_rice.gApplication_rice().getPackageName();
    private int score = 0;
    private static boolean isShow = false;
    private final ImageButton[] starIbs = new ImageButton[5];

    public StarActiveDialog_rice(AppCompatActivity activity){
        super(activity);
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_star_active_rice);

        setCancelable(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);

        findViewById(R.id.dialog_star_active_btn_notnow).setOnClickListener(v -> dismiss());

        TextView textTv = findViewById(R.id.dialog_star_active_tv_text);

        String content = ActiveWindowConfig_rice.getInstance_pretfacile().getContent();
        if (!TextUtils.isEmpty(content)) textTv.setText(content);

        starIbs[0] = findViewById(R.id.dialog_star_active_ib_star_1);
        starIbs[1] = findViewById(R.id.dialog_star_active_ib_star_2);
        starIbs[2] = findViewById(R.id.dialog_star_active_ib_star_3);
        starIbs[3] = findViewById(R.id.dialog_star_active_ib_star_4);
        starIbs[4] = findViewById(R.id.dialog_star_active_ib_star_5);

        findViewById(R.id.dialog_star_active_btn_rate).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint(("DIALOG_RATE_ACTIVE_RATE_CLICK").toUpperCase(),String.valueOf(score));

            if (score==0) {
                Toast.makeText(activity,activity.getString(R.string.star_dialog_0rate_tip),Toast.LENGTH_SHORT).show();
                return;
            }

            if (score>=4){
                if (AppShowInfo_rice.getInstance().showSysReview()){
                    AppReviewUtil.INSTANCE.launchReviewFlow(activity, aBoolean -> {
                        if (!aBoolean) activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                        dismiss();
                        return null;
                    });
                }else activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

            }else activity.startActivity(new Intent(activity, ServiceFeedbackActivity_rice.class));

            dismiss();
        });

        for (int i=0;i<starIbs.length;i++){
            ImageButton scoreIb = starIbs[i];
            final int fScore = i+1;
            if (scoreIb!=null) scoreIb.setOnClickListener(v -> setScore(fScore));
        }

    }

    private void setScore(int score){

        if (score>5) score=5;
        else if (score<1) score=1;

        this.score = score;

        HttpManager_rice.getInstance().saveUserBuriedPoint(("DIALOG_RATE_ACTIVE_SCORE").toUpperCase(),String.valueOf(score));

        for (int i=0;i<starIbs.length;i++){
            ImageButton scoreIb = starIbs[i];
            if (scoreIb==null) continue;
            scoreIb.setBackgroundResource(i<score ? R.drawable.icon_star_true:R.drawable.icon_star_false);
        }
    }

    @Override
    public void show() {
        if (isShow) return;
        isShow = true;
        super.show();
        HttpManager_rice.getInstance().saveUserBuriedPoint(("DIALOG_RATE_ACTIVE_SHOW").toUpperCase());
        UserInfoHelper_rice.getInstance().addStarActiveCount_rice();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        isShow = false;
    }

}
